package com.example.springboot.ytest.ytest.java8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StreamReduce {
	static class Test{
		Integer i;
		String s;
		
		public Test(Integer i, String s) {
			super();
			this.i = i;
			this.s = s;
		}

		public Integer getI() {
			return i;
		}

		public void setI(Integer i) {
			this.i = i;
		}

		public String getS() {
			return s;
		}

		public void setS(String s) {
			this.s = s;
		}

		@Override
		public String toString() {
			return "Test [i=" + i + ", s=" + s + "]";
		}
		
	}
	public static void main(String[] args) {
		List<Test> tests = new ArrayList<>();
//		tests.add(new Test(1,"a"));
//		tests.add(new Test(8,"b"));
//		tests.add(new Test(3,"f"));
//		tests.add(new Test(4,"d"));
//		tests.add(new Test(5,"e"));
//		tests.add(new Test(null,"e")); // 如果有空值会报错
		
//		long idLong = new ArrayList<Long>().stream().reduce(0L, Long::sum);
//		tests.stream().reduce(0, (person1,person2)->person1.getI()+person2.getI());
		
		// 如果是空数据将返回0
		Integer i3 = tests.stream().filter(e -> e.getI()!=null).mapToInt(Test::getI).sum(); // 先要过滤掉空值
		Integer i1 = tests.stream().collect(Collectors.summingInt(Test::getI));
		Integer i2 = tests.stream().mapToInt(Test::getI).sum();
		System.out.println(i1);
		System.out.println(i2);
	}
}
